home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 1 / Macwelt DVD 1.toast / Web-Publishing / HTML-Editoren / Alpha ƒ / Tcl / SystemCode / textFill.tcl < prev    next >
Encoding:
Text File  |  2001-01-04  |  7.7 KB  |  272 lines

  1. ####################################################################
  2. #  AlphaTcl - core Tcl engine
  3. # Much by Vince Darley.
  4. #                                    created: 26/11/96 {7:08:34 pm} 
  5. #                                last update: 01/04/2001 {22:46:50 PM}
  6. #  Author: Vince Darley
  7. #  E-mail: <vince@santafe.edu>
  8. #    mail: 317 Paseo de Peralta, Santa Fe, NM 87501, USA
  9. #     www: <http://www.santafe.edu/~vince/>
  10. #  
  11. ####################################################################
  12.  
  13. ## 
  14.  #       
  15.  # 'rememberWhereYouAre'
  16.  # 
  17.  #  Given the start of a paragraph and the point to remember, this returns a
  18.  #  record which must be passed to the following function so that it can
  19.  #  find the spot later, even after the paragraph has had
  20.  #  space/tabs/new-lines meddled with.  An optional last argument is a list
  21.  #  of other characters quoted so they are regexp insensitive, which should
  22.  #  also be ignored.  This is used so we can remember positions in text
  23.  #  which has cosmetic characters on the left/right which are not wrapped
  24.  #  (such as the hashes to the left here!).
  25.  #       
  26.  # 'goBackToWhereYouWere'
  27.  # 
  28.  #  Given the beginning and end of a selection, and a previous record, where
  29.  #  the beginning, and record correspond to a previous call of
  30.  #  'rememberWhereYouAre', this procedure will move the insertion point to
  31.  #  the correct place.
  32.  #       
  33.  ##
  34.  
  35. proc rememberWhereYouAre {startPara pos endPara {commentReg ""}} {
  36.     set start [pos::math $pos -20]
  37.     if {[pos::compare $start < $startPara]} {
  38.     set start $startPara
  39.     }
  40.     set __g_remember_str [getText $start $pos]
  41.     if {[string length [string trim $__g_remember_str]] < 3} {
  42.     # there wasn't much to remember; try the other way
  43.     set end [pos::math $pos +20]
  44.     if {[pos::compare $end > $endPara]} {
  45.         set end $endPara
  46.     }
  47.     set __g_remember_str [getText $pos $end]
  48.     set __g_remember_dir 0
  49.     } else {
  50.     set __g_remember_dir 1
  51.     }
  52.     
  53.     set __g_remember_str [quote::Regfind $__g_remember_str]
  54.     regsub -all "\[ \t\r\n${commentReg}\]+" $__g_remember_str \
  55.       {[ \t\r\n${commentReg}]+} __g_remember_str
  56.     return [list $__g_remember_str $__g_remember_dir]
  57. }
  58.  
  59. proc goBackToWhereYouWere {start end memory} {
  60.     if {[lindex $memory 0] != "" } {
  61.     regexp -indices ".*([lindex $memory 0]).*" [getText $start $end] \
  62.       "" submatch
  63.     if {[info exists submatch]} {
  64.         set p [pos::math $start + [lindex $memory 1] + \
  65.           [lindex $submatch [lindex $memory 1]]]
  66.     } else {
  67.         set p $end
  68.     }
  69.     goto [expr {[pos::compare $p >= $end] ? [pos::math $end - 1] : $p}]
  70.     } else {
  71.     goto $start
  72.     }
  73. }
  74.  
  75. ## 
  76.  # -------------------------------------------------------------------------
  77.  #     
  78.  #    "getLeadingIndent" --
  79.  #    
  80.  #  Find the indentation of the line containing 'pos', and convert it to a
  81.  #  minimal form of tabs followed by spaces.  If 'size' is given, then the
  82.  #  variable of that name is set to the length of the indent.  Similarly
  83.  #  'halftab' can be set to half a tab. 
  84.  # -------------------------------------------------------------------------
  85.  ##
  86. proc getLeadingIndent { pos {size ""} {halftab ""} } {
  87.     # get the leading whitespace of the current line
  88.     set res [search -s -n -f 1 -r 1 "^\[ \t\]*" [lineStart $pos]]
  89.     
  90.     # convert it to minimal form: tabs then spaces, stored in 'front'
  91.     getWinInfo a
  92.     set sp [string range "              " 1 $a(tabsize) ]
  93.     regsub -all "($sp| +\t)" [eval getText $res] "\t" front
  94.     if { $size != "" } {
  95.     upvar $size ind
  96.     # get the length of the indent
  97.     regsub -all "\t" $front $sp lfront
  98.     set ind [string length $lfront]
  99.     }
  100.     if { $halftab != "" } {
  101.     upvar $halftab ht
  102.     # get the length of half a tab
  103.     set ht [string range "            " 1 [expr {$a(tabsize)/2}]]
  104.     }
  105.     
  106.     return $front
  107. }
  108.  
  109.  
  110. proc getEndpts {} {
  111.     if {[pos::compare [getPos] == [selEnd]]} {
  112.     set start [getPos]
  113.     set finish [getMark]
  114.     if {[pos::compare $start > $finish]} {
  115.         set temp $start
  116.         set start $finish
  117.         set finish $temp
  118.     }
  119.     } else {
  120.     set start [getPos]
  121.     set finish [selEnd]
  122.     }
  123.     return [list $start $finish]
  124. }
  125.  
  126.  
  127. proc fillRegion {} {
  128.     global leftFillColumn
  129.     set ends [getEndpts]
  130.     set start [lineStart [lindex $ends 0]]
  131.     set finish [lindex $ends 1]
  132.     goto $start
  133.     set text [fillText $start $finish]
  134.     replaceText $start $finish [format "%$leftFillColumn\s" ""] $text "\r"
  135. }
  136.     
  137. proc wrapParagraph {} {
  138.     set pos [getPos]
  139.     set start [paragraph::start $pos] 
  140.     set finish [paragraph::finish $pos]
  141.     goto $start
  142.     wrapText $start $finish
  143.     goto $pos
  144. }
  145.  
  146. proc wrapRegion {} {
  147.     set ends [getEndpts]
  148.     set start [lineStart [lindex $ends 0]]
  149.     set finish [lindex $ends 1]
  150.     if {[pos::compare $start == $finish]} {
  151.     set finish [maxPos]
  152.     }
  153.     wrapText $start $finish
  154. }
  155.     
  156.  
  157.  
  158. # Remove text from window, transform, and insert back into window.
  159. proc fillText {from to} {
  160.     global doubleSpaces
  161.     set text [getText $from $to]
  162.     regexp "^\[ \t\]*" $text front
  163.     regsub -all "\[ \t\n\r\]+" [string trim $text] " " text
  164.     if {$doubleSpaces} {regsub -all {(([^.][a-z]|[^a-zA-Z@]|\\@)[.?!]("|'|'')?([])])?) } $text {\1  } text}
  165.     regsub -all " ?\[\r\n\]" [string trimright [breakIntoLines $text]] "\r${front}" text
  166.     return $front$text
  167. }
  168.  
  169. proc paragraphToLine {} {
  170.     global fillColumn
  171.     global leftFillColumn
  172.     set fc $fillColumn
  173.     set lc $leftFillColumn
  174.     set fillColumn 10000
  175.     set leftFillColumn 0
  176.     fillRegion
  177.     set fillColumn $fc
  178.     set leftFillColumn $lc
  179. }
  180.  
  181. proc lineToParagraph {} {
  182.     global fillColumn
  183.     global leftFillColumn
  184.     set fc $fillColumn
  185.     set fillColumn 75
  186.     set lc $leftFillColumn
  187.     set leftFillColumn 0
  188.     fillRegion
  189.     set fillColumn $fc
  190.     set leftFillColumn $lc
  191. }
  192.  
  193.  
  194. #set sentEnd {[.!?](\r|\n| +)}
  195. set sentEnd {(\r\r|\n\n|[.!?](\r|\n| +))}
  196. set sentBeg {[\r\n ][A-Z]}
  197.  
  198. proc sentenceRegion {} {
  199.     set ends [getEndpts]
  200.     set start [lineStart [lindex $ends 0]]
  201.     set finish [lindex $ends 1]
  202.     if {[pos::compare $start == $finish]} {
  203.     set finish [maxPos]
  204.     }
  205.     set t [string trim [getText $start $finish]]
  206.     set period [regexp {\.$} $t]
  207.     regsub -all "\[ \t\r\n\]+" $t " " text
  208.     regsub -all {\. } $text " " text
  209.     set result ""
  210.     foreach line [split [string trimright $text {.}] " "] {
  211.     if {[string length $line]} {
  212.         append result [breakIntoLines $line] ".\r"
  213.     }
  214.     }
  215.     if {!$period && [regexp {\.\r} $result]} {
  216.     set result [string trimright $result ".\r"]
  217.     append result "\r"
  218.     }
  219.     if {$result != [getText $start $finish]} {
  220.     replaceText $start $finish $result
  221.     }
  222.     goto $pos
  223. }
  224.  
  225. proc nextSentence {} {
  226.     global sentBeg sentEnd
  227.     if {![catch {search -s -f 1 -r 1 $sentEnd [getPos]} mtch]} {
  228.     if {![catch {search -s -f 1 -r 1 -i 0 $sentBeg [pos::math [lindex $mtch 1] - 1]} mtch]} {
  229.         goto [pos::math [lindex $mtch 0] + 1]
  230.     }
  231.     }
  232. }
  233.  
  234.  
  235. proc prevSentence {} {
  236.     global sentBeg sentEnd
  237.     if {[catch {search -s -f 0 -r 1 $sentBeg [pos::math [getPos] - 2]} mtch]} return
  238.     if {![catch {search -s -f 0 -r 1 $sentEnd [lindex $mtch 1]} mtch]} {
  239.     if {![catch {search -s -f 1 -r 1 -i 0 $sentBeg [pos::math [lindex $mtch 1] - 1]} mtch]} {
  240.         goto [pos::math [lindex $mtch 0] + 1]
  241.     }
  242.     }
  243. }
  244.  
  245. #===============================================================================
  246. # Called by Alpha to do "soft wrapping"
  247. proc softProc {pos start next} {
  248.     global leftFillColumn
  249.     goto $start
  250.     set finish [paraFinish $start]
  251.     set text [fillText $start $finish]
  252.     if {"${text}\r" != [getText $start $finish]} {
  253.     replaceText $start $finish [format "%$leftFillColumn\s" ""] $text "\r"
  254.     return 1
  255.     } else {
  256.     return 0
  257.     }
  258. }
  259.  
  260. proc dividingLine {} {
  261.     global mode
  262.     global ${mode}modeVars
  263.     if {[info exists ${mode}modeVars(prefixString)]} {
  264.     set a [string trim [set ${mode}modeVars(prefixString)]]
  265.     } else {
  266.     set a "#"
  267.     }
  268.     insertText "${a}===============================================================================\r"
  269. }
  270.